<?php
!defined('M_COM') && exit('No Permisson');
$_mdebug = new cls_debug;
class cls_mysql{
	var $link;
	var $name = '';
	function connect($dbhost,$dbuser,$dbpw,$dbname = '',$pconnect = 0,$halt = TRUE,$ncharset = ''){
		$func = !$pconnect ? 'mysql_connect' : 'mysql_pconnect';
		if(!$this->link = @$func($dbhost,$dbuser,$dbpw,1)){
			if($halt){
				$this->halt('Can not connect to MySQL server');
			}else return false;
		}else{
			if($this->version() > '4.1'){
				global $dbcharset,$mcharset;
				$ncharset = empty($ncharset) ? (empty($dbcharset) ? str_replace('-','',strtolower($mcharset)) : $dbcharset) : $ncharset;
				$serverset = $ncharset ? 'character_set_connection='.$ncharset.', character_set_results='.$ncharset.', character_set_client=binary' : '';
				$serverset .= $this->version() > '5.0.1' ? ((!$serverset ? '' : ',').'sql_mode=\'\'') : '';
				$serverset && mysql_query("SET $serverset", $this->link);
			}
			if($dbname && !@mysql_select_db($dbname, $this->link)){
				if($halt){
					$this->halt("Can not select database $dbname");
				}else return false;
			}
			$this->name = $dbhost.'_'.$dbname;
		}
		return true;
	}
	function select_db($dbname){
		return mysql_select_db($dbname, $this->link);
	}
	function query($sql, $type = ''){
		global $_mdebug;
		$dbstart = mmicrotime();
		$func = ($type == 'UNBUFFERED' && @function_exists('mysql_unbuffered_query')) ? 'mysql_unbuffered_query' : 'mysql_query';
		if(!($query = $func($sql, $this->link))){
			if(in_array($this->errno(), array(2006, 2013)) && substr($type, 0, 5) != 'RETRY'){
				$this->close();
				require M_ROOT.'./base.inc.php';
				$this->connect($dbhost,$dbuser,$dbpw,$dbname,$pconnect,true,$dbcharset);
				return $this->query($sql,'RETRY'.$type);
			} elseif($type != 'SILENT' && substr($type, 5) != 'SILENT'){
				$this->halt('MySQL Query Error', $sql);
			}
		}
		$_mdebug->add($sql,mmicrotime()-$dbstart,$this->name);
		return $query;
	}
	function fetch_array($query, $result_type = MYSQL_ASSOC){
		return mysql_fetch_array($query, $result_type);
	}
	function ex_fetch_array($sql,$ttl = 0){
		$re = ex_get_cache($this->name,$sql,$ttl);
		if($re === false){
			$query = $this->query($sql);
			$re = array();
			while($r = $this->fetch_array($query)) $re[] = $r;
			$this->free_result($query);
			$ttl && ex_set_cache($re,$this->name,$sql,$ttl);
		}
		return $re ? $re : array();
	}
	function fetch_one($sql,$ttl = 0){
		$re = ex_get_cache($this->name,$sql,$ttl);
		if($re === false){
			$query = $this->query($sql);
			$re = $this->fetch_array($query);
			$this->free_result($query);
			$ttl && ex_set_cache($re,$this->name,$sql,$ttl);
		}
		return $re ? $re : array();
	}
	function result_one($sql,$ttl = 0) {
		$re = ex_get_cache($this->name,$sql,$ttl);
		if($re === false){
			$query = $this->query($sql);
			$re = $this->result($query,0);
			$this->free_result($query);
			$ttl && ex_set_cache($re,$this->name,$sql,$ttl);
		}
		return $re ? $re : '';
	}
	function affected_rows(){
		return mysql_affected_rows($this->link);
	}
	function error(){
		return $this->link ? mysql_error($this->link) : mysql_error();
	}
	function errno(){
		return intval($this->link ? mysql_errno($this->link) : mysql_errno());
	}
	function result($query, $row){
		return @mysql_result($query, $row);
	}
	function num_rows($query){
		return mysql_num_rows($query);
	}
	function num_fields($query){
		return mysql_num_fields($query);
	}
	function free_result($query){
		return mysql_free_result($query);
	}
	function insert_id(){
		return ($id = mysql_insert_id($this->link)) >= 0 ? $id : $this->result($this->query("SELECT last_insert_id()"), 0);
	}
	function fetch_row($query){
		return mysql_fetch_row($query);
	}
	function fetch_fields($query){
		return mysql_fetch_field($query);
	}
	function version(){
		return mysql_get_server_info($this->link);
	}
	function close(){
		return mysql_close($this->link);
	}
	function halt($message = '', $sql = ''){
		global $timestamp,$tblprefix,$_no_dbhalt;
		if(empty($_no_dbhalt)) include M_ROOT.'./include/mysql.err.php';
	}
}
class cls_debug{
	var $uri = '';
	var $tpl = '';
	var $tag = '';
	var $querys = 0;
	var $wtime = 0;
	var $records = 0;
	var $counted = 0;
	function __construct(){
		$this->cls_debug();
	}
	function cls_debug(){
	}
	function init(){
		$this->uri = $this->tpl = $this->tag = '';
		$this->querys = $this->wtime = $this->records = $this->counted = 0;
	}
	function setvar($name,$val){
		$this->$name = $val;
	}
	function add($sql,$time,$name){
		global $db,$tblprefix,$timestamp,$debugenabled,$debugadmin,$debuglow,$viewdebug;
		if($viewdebug){
			$this->querys ++;
			$this->wtime += $time;
		}
		if($debugenabled && $time > (empty($debuglow) ? 0 : $debuglow) / 1000){
			if(!$debugadmin && (defined('M_ADMIN') || defined('M_MCENTER'))) return;
			if(!$this->counted){
				$this->records = mysql_result(mysql_query("SELECT COUNT(*) FROM {$tblprefix}dbdebugs",$db->link),0);
				$this->counted = 1;
			}
			if(++$this->records < 500000){
				@mysql_query("INSERT INTO {$tblprefix}dbdebugs SET 
				ddid='".(md5($name.$sql.$this->tpl))."',
				ddsql='".addslashes($sql)."',
				ddtbl='".addslashes($name)."',
				ddused='".round(1000 * $time,2)."',
				ddurl='".addslashes($this->uri ? $this->uri : M_URI)."',
				ddtpl='".addslashes($this->tpl)."',
				ddtag='".addslashes($this->tag)."',
				ddate='$timestamp'"
				,$db->link);
			}
		}
	}
	function view(){
		global $viewdebugmode;
		$info = 'querys:'.$this->querys.',in:'.round($this->wtime,4).'s';
		echo $viewdebugmode=='direct' ? "\n$info" : "\n<!--$info-->";
	}
}

?>